/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.imp;

import dao.CategoriaDao;
import hibernate.HibernateUtil;
import java.util.ArrayList;
import modelo.dominio.Categoria;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Pablo
 */
public class CategoriaDaoImp implements CategoriaDao {

    @Override
    public void insert(Categoria categoria) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(categoria);
        session.getTransaction().commit();
        session.close();
        
    }

    @Override
    public void delete(Categoria categoria) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(categoria);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public ArrayList<Categoria> getAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Categoria.class);
        criteria.addOrder(Order.asc("id"));
        ArrayList<Categoria> lista = (ArrayList<Categoria>) criteria.list();
        session.close();
        return lista;
    }

    @Override
    public Categoria getCategira(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Categoria.class);
        criteria.add(Restrictions.eq("id", id));
        Categoria cat = (Categoria) criteria.list().get(0);
        session.close();
        return cat;
    }

    @Override
    public void update(Categoria categoria) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(categoria);
        session.getTransaction().commit();
        session.close();
    }
    
}
